<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="user_id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="user_id"></el-input>
    </el-form-item>
    <el-form-item label="博文标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="博文标题"></el-input>
    </el-form-item>
    <el-form-item label="博文内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="博文内容"></el-input>
    </el-form-item>
    <el-form-item label="浏览量" prop="views">
      <el-input v-model="dataForm.views" placeholder="浏览量"></el-input>
    </el-form-item>
    <el-form-item label="评论总数" prop="commentCount">
      <el-input v-model="dataForm.commentCount" placeholder="评论总数"></el-input>
    </el-form-item>
    <el-form-item label="发表时间" prop="firstDate">
      <el-input v-model="dataForm.firstDate" placeholder="发表时间"></el-input>
    </el-form-item>
    <el-form-item label="喜欢数量" prop="likeCount">
      <el-input v-model="dataForm.likeCount" placeholder="喜欢数量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          title: '',
          content: '',
          views: '',
          commentCount: '',
          firstDate: '',
          likeCount: ''
        },
        dataRule: {
          userId: [
            { required: true, message: 'user_id不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '博文标题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '博文内容不能为空', trigger: 'blur' }
          ],
          views: [
            { required: true, message: '浏览量不能为空', trigger: 'blur' }
          ],
          commentCount: [
            { required: true, message: '评论总数不能为空', trigger: 'blur' }
          ],
          firstDate: [
            { required: true, message: '发表时间不能为空', trigger: 'blur' }
          ],
          likeCount: [
            { required: true, message: '喜欢数量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/blog/articles/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.articles.userId
                this.dataForm.title = data.articles.title
                this.dataForm.content = data.articles.content
                this.dataForm.views = data.articles.views
                this.dataForm.commentCount = data.articles.commentCount
                this.dataForm.firstDate = data.articles.firstDate
                this.dataForm.likeCount = data.articles.likeCount
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/blog/articles/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'title': this.dataForm.title,
                'content': this.dataForm.content,
                'views': this.dataForm.views,
                'commentCount': this.dataForm.commentCount,
                'firstDate': this.dataForm.firstDate,
                'likeCount': this.dataForm.likeCount
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
